package com.eric.learning.a.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName FuseController.java
 * @Description 熔断测试
 * @createTime 2020年07月13日 20:53:00
 */
@RestController
public class FuseController {

    @Autowired
    RestTemplate restTemplate;

    @Value("${service-urls.service-b}")
    String serviceB;

    @GetMapping("/exception")
    @SentinelResource(value = "exception" ,fallback = "fallbackHandler")
    public String getBException(){
        return restTemplate.getForObject(serviceB+"/exception",String.class);
    }

    @GetMapping("/timeout")
    @SentinelResource(value = "timeout" ,fallback = "fallbackHandler")
    public String getBTimeout(){
        return restTemplate.getForObject(serviceB+"/timeout",String.class);
    }

    public String fallbackHandler(/* 入参 */){
        return "熔断处理";
    }
}
